package com.tsky.system.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tsky.casing.QueryBean;
import com.tsky.casing.action.BackAction;
import com.tsky.common.ActionUtil;
import com.tsky.common.BeanUtil;
import com.tsky.system.entity.Role;
import com.tsky.system.entity.User;
import com.tsky.system.json.JsonFormat;
import com.tsky.system.manager.UserManager;
import com.tsky.system.query.UserQuery;

@Component("userAction")
@Scope("prototype")
public class UserAction extends BackAction {
	@Autowired
	private UserManager entityManager;

	/* bean properties */
	private String id;
	private String status;
	private String name;
	private String code;
	private String psw;
	private String departid;
	private String sex;
	private String phone;
	private String mobile;
	private String email;
	private String address;
	private String others;
	private String theme;// 用户主题

	private String ids;
	private String roleid;
	private String newpsw;
	private String isChargor;
	
	private String isConfiged;//是否已配置
	private String projectId;//产品id

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getIsConfiged() {
		return isConfiged;
	}

	public void setIsConfiged(String isConfiged) {
		this.isConfiged = isConfiged;
	}

	/**
	 * 登录
	 * 
	 * @author: cainiao
	 * @time: 2010-6-13 上午07:04:40
	 * @function:
	 */
	public void login() {
		User loginUser = (User) entityManager.userLogin(code, psw);
		ActionUtil.setToSession("loginUser", loginUser);
		boolean result = !(loginUser == null);

		ActionUtil.ajaxWrite(result);
	}

	/**
	 * ajax用户Code唯一校验
	 * 
	 * @author: cainiao
	 * @time: 2010-6-13 上午07:04:49
	 * @function:
	 */
	public void isUniqueCode() {
		Object user = entityManager.isUniqueCode(code);
		boolean result = (user == null);
		ActionUtil.ajaxWrite(result);
	}

	/**
	 * 注销
	 * 
	 * @author: cainiao
	 * @time: 2010-6-13 上午07:05:10
	 * @function:
	 * @return
	 */
	public String loginOut() {
		ActionUtil.getSession().removeAttribute("loginUser");
		return "loginOut";
	}

	/**
	 * 修改密码
	 * 
	 * @author: cainiao
	 * @time: 2010-6-13 上午07:05:24
	 * @function:
	 */
	public void editPsw() {
		boolean result = false;
		User user = (User) ActionUtil.getAttribute("loginUser");
		if (psw.equals(user.getPsw())) {
			user.setPsw(newpsw);
			result = entityManager.updateEntity(user);
			ActionUtil.ajaxWrite(result);
		}else{
			ActionUtil.ajaxWrite("输入的密码不正确!");
		}
	}

	/**
	 * 获取系统角色
	 * 
	 * @author: cainiao
	 * @time: 2010-6-13 上午07:05:39
	 * @function:
	 */
	public void getRoles() {
		List<?> roleList = entityManager.getAllEntity(Role.class, "1");
		Object json = JsonFormat.userRoleToJson(roleList, roleid);
		ActionUtil.ajaxWrite(json);
	}

	/**
	 * 
	 * @function: 获取性别下拉选项
	 * @author: cainiao
	 * @time: 2011-12-15 下午01:22:15
	 */
	public void getSexs() {
		Object json = JsonFormat.userSexToJson(sex);
		ActionUtil.ajaxWrite(json);
	}

	public void grid() {
		QueryBean grid = new UserQuery();
		BeanUtil.copyProperties(grid, this);

		entityManager.query(grid);

		String jsonData = JsonFormat.userToJson(grid);
		ActionUtil.ajaxWrite(jsonData);
	}

	public void add() {
		User entity = new User();
		BeanUtil.copyProperties(entity, this);
		entity.setId(BeanUtil.getID());
		entity.setCreateTime(BeanUtil.getTimeStamp());

		if (!BeanUtil.isEmpty(roleid)) {
			Role role = entityManager.getEntity(Role.class, roleid);
			entity.setRole(role);
		}

		boolean result = entityManager.saveEntity(entity);
		ActionUtil.ajaxWrite(result);
	}

	public void delete() {
		String[] idset = ids.split(",");
		boolean result = entityManager.delEntity(User.class, idset);
		ActionUtil.ajaxWrite(result);
	}

	public String query() {
		User entity = entityManager.getEntity(User.class, id);
		ActionUtil.setToEL("userEnt", entity);
		return "query";
	}

	public void update() {
		User entity = entityManager.getEntity(User.class, id);
		BeanUtil.copyProperties(entity, this);

		if (!BeanUtil.isEmpty(roleid)) {
			Role role = entityManager.getEntity(Role.class, roleid);
			entity.setRole(role);
		}

		boolean result = entityManager.updateEntity(entity);
		ActionUtil.ajaxWrite(result);
	}
	
	public void fuzzyQuery() {
		isChargor = "0";
		QueryBean grid = new UserQuery();
		BeanUtil.copyProperties(grid, this);
		
		entityManager.query(grid);

		String jsonData = JsonFormat.userListToJson(grid);
		ActionUtil.ajaxWrite(jsonData);
	}
	
	/**
	 * 查询所有负责人
	 */
	public void queryAllChargor() {
		isChargor = "0";
		QueryBean grid = new UserQuery();
		BeanUtil.copyProperties(grid, this);
		
		entityManager.queryAllChargor(grid);

		String jsonData = JsonFormat.userListToJson(grid);
		ActionUtil.ajaxWrite(jsonData);
	}
	
	/**
	 * 用于产品配置开发人员时查询
	 */
//	public void queryUser() {
//		QueryBean queryBean = new UserQuery();
//		BeanUtil.copyProperties(queryBean, this);
//		
//		entityManager.queryUser(queryBean);
//		
//		String jsonData = JsonFormat.proUserListToJson(queryBean,
//				this.projectId, proDevRLSManager);
//		ActionUtil.ajaxWrite(jsonData);
//	}
	
	/**
	 * 
	 * @author：cainiao
	 * @方法描述：修改用户主题
	 * @date: 2011-3-14
	 */
	public void changeTheme() {
		User entity = entityManager.getEntity(User.class, ActionUtil.getUserID());
		entity.setTheme(theme);

		boolean result = entityManager.updateEntity(entity);
		if(result){
			ActionUtil.getSession().removeAttribute("loginUser");
			ActionUtil.setToSession("loginUser", entity);
		}
		ActionUtil.ajaxWrite(result);
	}

	/* spring DI */
	public UserManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(UserManager entityManager) {
		this.entityManager = entityManager;
	}

	/* simple getter and setter */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getPsw() {
		return psw;
	}

	public void setPsw(String psw) {
		this.psw = psw;
	}

	public String getDepartid() {
		return departid;
	}

	public void setDepartid(String departid) {
		this.departid = departid;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getOthers() {
		return others;
	}

	public void setOthers(String others) {
		this.others = others;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getRoleid() {
		return roleid;
	}

	public void setRoleid(String roleid) {
		this.roleid = roleid;
	}

	public String getNewpsw() {
		return newpsw;
	}

	public void setNewpsw(String newpsw) {
		this.newpsw = newpsw;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getIsChargor() {
		return isChargor;
	}

	public void setIsChargor(String isChargor) {
		this.isChargor = isChargor;
	}

}
